package org.tom.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.PageList;
import org.tom.org.domain.DataDictionaries;
import org.tom.org.domain.Department;
import org.tom.org.query.DataDictionariesQuery;
import org.tom.org.service.IDataDictionariesService;

import java.util.List;

@RestController
@RequestMapping("/data")
@Api(tags = "数据字典",description = "数据字典详细描述")
public class DataDictionariesController {
    @Autowired
    private IDataDictionariesService dataDictionariesService;

    @PostMapping
    @ApiOperation(value = "分页显示",notes = "传输分页需要的当前页和每页显示的条数")
    public PageList<DataDictionaries> quepage(@RequestBody DataDictionariesQuery dataDictionariesQuery){
        return dataDictionariesService.queryList(dataDictionariesQuery);
    }

    @ApiOperation(value = "字典删除",notes = "通过id进行删除")
    @DeleteMapping("{id}") // 删除携带参数id
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            dataDictionariesService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping // 修改和添加
    public JsonResult updateAndSave(@RequestBody DataDictionaries dataDictionaries){
        System.out.println(dataDictionaries);
        try {
            if (dataDictionaries.getId() == null){
                dataDictionariesService.insert(dataDictionaries);
            }else {
                dataDictionariesService.update(dataDictionaries);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

}
